<template>
	<view
		class="u-progress"
		:style="{
			borderRadius: round ? '100rpx' : 0,
			height: height + 'rpx',
			backgroundColor: inactiveColor
		}"
	>
		<view class="u-percent">
			<slot v-if="$slots.default" />
			<block v-else-if="showPercent">{{ isNaN(percent) ? 0 : percent }}%</block>
		</view>
		<view :class="[type ? `u-type-${type}-bg` : '', striped ? 'u-striped' : '', striped && stripedActive ? 'u-striped-active' : '']" class="u-active" :style="[progressStyle]">
			<view class="u-text">{{ text }}</view>
			<view v-if="Array.isArray(activeColor)" class="u-pro" :style="'border-color: ' + activeColor[0]"><text :style="'background: ' + activeColor[1]"></text></view>
		</view>
	</view>
</template>

<script>
/**
 * lineProgress 线型进度条
 * @description 展示操作或任务的当前进度，比如上传文件，是一个线形的进度条。
 * @tutorial https://www.uviewui.com/components/lineProgress.html
 * @property {String Number} percent 进度条百分比值，为数值类型，0-100
 * @property {Boolean} round 进度条两端是否为半圆（默认true）
 * @property {String} type 如设置，active-color值将会失效
 * @property {String} active-color 进度条激活部分的颜色（默认#19be6b）
 * @property {String} inactive-color 进度条的底色（默认#ececec）
 * @property {Boolean} show-percent 是否在进度条内部显示当前的百分比值数值（默认true）
 * @property {String Number} height 进度条的高度，单位rpx（默认28）
 * @property {Boolean} striped 是否显示进度条激活部分的条纹（默认false）
 * @property {Boolean} striped-active 条纹是否具有动态效果（默认false）
 * @example <u-line-progress :percent="70" :show-percent="true"></u-line-progress>
 */
export default {
	name: 'u-line-progress',
	props: {
		// 两端是否显示半圆形
		round: {
			type: Boolean,
			default: false
		},
		// 主题颜色
		type: {
			type: String,
			default: ''
		},
		// 激活部分的颜色
		activeColor: {
			type: [Array, String],
			default: '#19be6b'
		},
		inactiveColor: {
			type: String,
			default: '#ececec'
		},
		// 进度百分比，数值
		percent: {
			type: Number,
			default: 0
		},
		// 是否在进度条内部显示百分比的值
		showPercent: {
			type: Boolean,
			default: false
		},
		// 进度条的高度，单位rpx
		height: {
			type: [Number, String],
			default: 12
		},
		text: {
			type: [Number, String],
			default: ''
		},
		// 是否显示条纹
		striped: {
			type: Boolean,
			default: false
		},
		// 条纹是否显示活动状态
		stripedActive: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {};
	},
	computed: {
		progressStyle() {
			let style = {};
			style.width = this.percent + '%';
			if (typeof this.activeColor == 'string') {
				style.backgroundColor = this.activeColor;
			} else {
				style.background = `linear-gradient(to right, ${this.activeColor[0]} 0%, ${this.activeColor[1]} 100%)`;
			}
			return style;
		}
	},
	methods: {}
};
</script>

<style lang="scss" scoped>
@import '../../libs/css/style.components.scss';

.u-progress {
	position: relative;
	height: 28rpx;
	display: inline-flex;
	align-items: center;
	width: 100%;
	border-radius: 100rpx;
}

.u-active {
	position: relative;
	width: 0;
	height: 100%;
	align-items: center;
	@include vue-flex;
	justify-items: flex-end;
	justify-content: space-around;
	font-size: 14px;
	color: #000;
	transition: all 0.4s ease;

	.u-text {
		position: absolute;
		right: 0;
		top: -44rpx;
		font-size: 14px;
		color: #666;
		transform: translateX(50%);
	}

	.u-pro {
		position: absolute;
		right: -20rpx;
		border-radius: 50%;
		border: 1px solid #19be6b;

		text {
			background: #19be6b;
			width: 20rpx;
			height: 20rpx;
			padding: 0;
			display: block;
			min-width: 0;
			border-radius: 50%;
			margin: 6rpx;
		}
	}
}

.u-percent {
	position: absolute;
	left: 0;
	right: 0;
	top: 0;
	bottom: 0;
	text-align: center;
	font-size: 14px;
	line-height: 28rpx;
}

.u-striped {
	background-image: linear-gradient(45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
	background-size: 39px 39px;
}

.u-striped-active {
	animation: progress-stripes 2s linear infinite;
}

@keyframes progress-stripes {
	0% {
		background-position: 0 0;
	}

	100% {
		background-position: 39px 0;
	}
}
</style>
